{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.metrics import r2_score\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import  RidgeCV\n",
    "from sklearn.linear_model import LassoCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   instant      dteday      temp     atemp       hum  windspeed  season_1  \\\n",
      "0        1  2011-01-01  0.344167  0.363625  0.805833   0.160446         1   \n",
      "1        2  2011-01-02  0.363478  0.353739  0.696087   0.248539         1   \n",
      "2        3  2011-01-03  0.196364  0.189405  0.437273   0.248309         1   \n",
      "3        4  2011-01-04  0.200000  0.212122  0.590435   0.160296         1   \n",
      "4        5  2011-01-05  0.226957  0.229270  0.436957   0.186900         1   \n",
      "\n",
      "   season_2  season_3  season_4    ...     weekday_4  weekday_5  weekday_6  \\\n",
      "0         0         0         0    ...             0          0          1   \n",
      "1         0         0         0    ...             0          0          0   \n",
      "2         0         0         0    ...             0          0          0   \n",
      "3         0         0         0    ...             0          0          0   \n",
      "4         0         0         0    ...             0          0          0   \n",
      "\n",
      "   workingday_0  workingday_1  weathersit_1  weathersit_2  weathersit_3   cnt  \\\n",
      "0             1             0             0             1             0   985   \n",
      "1             1             0             0             1             0   801   \n",
      "2             0             1             1             0             0  1349   \n",
      "3             0             1             1             0             0  1562   \n",
      "4             0             1             1             0             0  1600   \n",
      "\n",
      "    log_cnt  \n",
      "0  6.893656  \n",
      "1  6.687109  \n",
      "2  7.207860  \n",
      "3  7.354362  \n",
      "4  7.378384  \n",
      "\n",
      "[5 rows x 40 columns]\n",
      "       temp     atemp       hum  windspeed  season_1  season_2  season_3  \\\n",
      "0  0.344167  0.363625  0.805833   0.160446         1         0         0   \n",
      "1  0.363478  0.353739  0.696087   0.248539         1         0         0   \n",
      "2  0.196364  0.189405  0.437273   0.248309         1         0         0   \n",
      "3  0.200000  0.212122  0.590435   0.160296         1         0         0   \n",
      "4  0.226957  0.229270  0.436957   0.186900         1         0         0   \n",
      "\n",
      "   season_4  yr_0  yr_1      ...       weekday_2  weekday_3  weekday_4  \\\n",
      "0         0     1     0      ...               0          0          0   \n",
      "1         0     1     0      ...               0          0          0   \n",
      "2         0     1     0      ...               0          0          0   \n",
      "3         0     1     0      ...               1          0          0   \n",
      "4         0     1     0      ...               0          1          0   \n",
      "\n",
      "   weekday_5  weekday_6  workingday_0  workingday_1  weathersit_1  \\\n",
      "0          0          1             1             0             0   \n",
      "1          0          0             1             0             0   \n",
      "2          0          0             0             1             1   \n",
      "3          0          0             0             1             1   \n",
      "4          0          0             0             1             1   \n",
      "\n",
      "   weathersit_2  weathersit_3  \n",
      "0             1             0  \n",
      "1             1             0  \n",
      "2             0             0  \n",
      "3             0             0  \n",
      "4             0             0  \n",
      "\n",
      "[5 rows x 36 columns]\n"
     ]
    }
   ],
   "source": [
    "df =pd.read_csv(\"sharingbike.csv\")\n",
    "print(df.head())\n",
    "y = df[\"cnt\"]\n",
    "X = df.drop([\"instant\",\"dteday\",\"cnt\",\"log_cnt\"],axis = 1)\n",
    "print(X.head())\n",
    "\n",
    "feat_names = X.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(584, 36)\n"
     ]
    }
   ],
   "source": [
    "#随机选取20%的数据为测试样本，其余为训练样本\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=33,test_size=0.2)\n",
    "print(X_train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7232. 5392. 6160. 4304. 3824. 3408. 4992. 4016. 4624. 3632. 4640. 5552.\n",
      " 7088. 5424. 3824.  624.  928. 4288. 5872. 7760. 3184. 4736.  944. 4544.\n",
      " 1008. 1856. 2992. 4912.  496. 4464. 3184. 5856. 3856.  400. 4944. 7088.\n",
      " 5264. 3024. 7536. 4688. 1840. 1104. 6800. 3024. 3408. 3072. 3984. 5104.\n",
      " 5008. 6384. 6064. 6704. 3632. 1168. 4016. 5120. 7136. 2432. 3856. 1136.\n",
      " 2576. 3280. 1040. 4832. 2352. 6784. 3696. 3440. 6848. 4560. 4784. 5744.\n",
      " 3696.  896. 2656. 3904. 4784. 5296. 3360. 4736. 1824. 7184. 6992. 1968.\n",
      " 1552. 5360. 4240. 6608. 7072. 1648. 3696. 5248. 4592. 5200. 2640. 4032.\n",
      " 1888. 2512. 5392. 4768. 7440. 4528. 5296. 4160. 3408. 6336. 6672. 4560.\n",
      " 6224. 6912. 2336. 7024. 7184. 6224. 4640. 6160. 7248. 7376. 6224. 3600.\n",
      " 3856. 2672. 4960. 4976. 6640. 7216. 4176. 5664. 2064. 4080. 6576. 3568.\n",
      " 5200. 4592. 4752. 7712. 5984. 2512. 3760. 5728. 1648. 4912. 2288. 4000.\n",
      " 3056. 1840. 4848.]\n",
      "[ 6944.  4736.  3760.  2896.  4528.  7664.  5264.  2768.  5024.  1152.\n",
      "  3472.  3728.  4400.  5632.  1680.  4016.  7104.  1168.  6160.  5808.\n",
      "  4240.  3088.  3248.  5776.  4208.  5808.  4912.  3216.  1744.  5280.\n",
      "  4592.  1744.  6448.  3456.  5552.  4656.  3888.  4368.  6192.  4752.\n",
      "  5840.  7760.  6544.  5520.  6384.  6240.  6656.  6896.  5040.  5664.\n",
      "  6800.  7376.  5520.   144.  5232.  5456.  5136.  6288.  3664.  3552.\n",
      "  3232.  4144.  4272.  3152.  1024.  3632.  3840.  4816.  3584.  2816.\n",
      "  7296.  4784.  6288.  5344.  6608.  3808.  6432.  4048.  3728.  3344.\n",
      "  5744.  6592.  3440.  3600.  6448.  7344.  4976.  3824.  3600.  6000.\n",
      "  3904.  7232.  4416.   672.  6608.  4176.  6352.  4336.  1424.  2384.\n",
      "  3024.  5024.  5680.  4160.  6544.  2640.  6224.  6928.  4528.  4416.\n",
      "  5072.  4144.  2032.  4992.  3056.  6576.  3984.  6992.  3824.  5088.\n",
      "   784.  6864.  7216.  3568.  3264.  6080.  4272.  5200.  1712.  4336.\n",
      "  5360.  4976.  6944.  4944.  6416.  5552.  6000.  6688.  4752.  2224.\n",
      "  4912.  4848.  1360.  4816.  3664.  7440.  6704.  3600.  4736.  6976.\n",
      "  3632.  3552.  3536.  4304.  4080.  2608.  2160.   912.  6064.  7136.\n",
      "  2560.  4528.  3216.  5456.  3280.  4944.  4304.  6256.  5792.  3680.\n",
      "  3824.  5680.  5136.  3584.  7184.  6384.  6080.  6528.  5152.  4944.\n",
      "  1056.  5904.  2768.  5840.  5264.  6864.  5264.  7024.   912.  5376.\n",
      "  3632.  4528.   784.  6672.  1888.  5072.  4464.  4560.  4272.  6896.\n",
      "  5360.  3968.  5360.  1264.  1184.  2976.  6448.  4272.  4176.  1040.\n",
      "  2288.  6416.  5040.  4912.  2688.  3504.  1472.  6464.  2352.  2832.\n",
      "  5536.  4048.  6592.  5120.  4672.  6496.  7680.  5408.  5264.  4976.\n",
      "  1888.  5584.  6112.  4928.  4752.  6672.  6432.  7312.  4432.  6352.\n",
      "  6672.  5808.  6448.  5520.  6768.  4400.  4208.  5680.  4672.  5520.\n",
      "  3360.  2416.  6896.  5552.  3664.  2448.  4304.  5232.  1888.  3440.\n",
      "  2000.  3408.  2384.  4672.  2928.  4512.  7216.  4240.  7664.  4192.\n",
      "  4672.  1264.  6416.  3216.  5920.  3440.  6480.   768.  4656.  5616.\n",
      "  5248.  5408.  5648.  4368.  4912.  3760.  2928.  1776.  3664.  5840.\n",
      "  2944.  5456.  3024.  4224.  3040.  7280.  4864.  2848.  2592.  7280.\n",
      "  3920.  2640.  1520.  4496.  5632.  1328.  7120.  6960.  6128.  3504.\n",
      "  5808.  7008.  7056.  3664.  7136.  5808.  6304.  4752.  5168.  4000.\n",
      "  5840.  4368.  1136.  6064.  1392.  2864.  6720.  5152.  3472.  5296.\n",
      "  2864.  5840.  2624.  4976.   880.  5808.  4016.  3472.  6064.  6480.\n",
      "  4496.  4384.  4432.  7072.  3024.  4528.  3856.  6864.  4752.  4560.\n",
      "  5264.  3120.  4144.  5584.  6928.  1232.  5376.  3280.  6064.  5360.\n",
      "  7280.  5776.  5248.  5136.  6448.  2800.  2864.  6880.  3584.  3152.\n",
      "  6000.  6944.  4336.  4432.  4560.  4464.  5712.  3728.  4720.  5328.\n",
      "  5328. -1360.  5568.  6192.  6704.  4320.  6480.  1072.   752.  5312.\n",
      "  5200.  1648.  3776.  4800.  7536.  2912.  3312.  5472.  4240.  3888.\n",
      "  6624.  4624.  6768.  4304.  4912.  5744.  5328.  4400.  1472.  4288.\n",
      "  1072.  2320.  4816.  2896.  4112.  7056.  1280.  5296.  4304.  6992.\n",
      "  5248.  3440.  6768.  3712.  1488.  5040.  2896.  2384.  5600.  3888.\n",
      "  5648.  2640.  1680.  3840.  5264.  4048.   512.  5072.  5920.  4272.\n",
      "  7264.  2704.  6768.  7504.  6224.  3152.  5232.  1136.  1440.  5136.\n",
      "  4976.  3280.  1856.  4816.  4048.  5392.  1600.  3600.  1120.  6928.\n",
      "  4704.  5264.  8016.  3728.  1248.  2336.  6064.  4976.  4352.  4048.\n",
      "  4704.  1296.  3936.  2320.  6064.  3216.  1264.  4272.  3120.  2608.\n",
      "  6368.  4576.  4048.  6544.  7136.  3040.  1904.  5152.   592.  3984.\n",
      "  4272.  4432.  6560.  5232.  6928.  7312.  5168.  6960.  2000.   384.\n",
      "  5488.  2928.  2576.   720.  3504.   848.  7024.  5312.  6288.  3568.\n",
      "  2160.  3888.  5104.  7216.  4432.  3152.  6784.  4128.  6544.  2848.\n",
      "  1008.  1600.  3872.  2960.  5024.  4080.  2800.  3536.  6880.  7280.\n",
      "  4624.  3216.  6528.  2448.  1648.  6064.  4176.  6544.  5328.  1200.\n",
      "  6416.  2160.  3360.  4864.  5136.  4000.  4880.  7408.  5616.  5584.\n",
      "  4624.  7696.  5872.  5040.  6496.  7216.  6736.   624.  6800.  2544.\n",
      "  7056.  3280.  2976.  3408.  5968.  4816.  6752.  6976.  7648.  4304.\n",
      "  5040.  7088.  7824.  4224.  2352.  2768.  4304.  1808.  4240.  7152.\n",
      "  7024.  2864.  3392.  1168.]\n"
     ]
    }
   ],
   "source": [
    "#尝试缺省参数的线性回归\n",
    "lr = LinearRegression()\n",
    "lr.fit(X_train,y_train)\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "print(y_test_pred_lr)\n",
    "print(y_train_pred_lr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         columns          coef\n",
      "0           temp  3.153339e+03\n",
      "1          atemp  1.335205e+03\n",
      "2            hum -1.334883e+03\n",
      "3      windspeed -2.491817e+03\n",
      "4       season_1  4.504145e+14\n",
      "5       season_2  4.504145e+14\n",
      "6       season_3  4.504145e+14\n",
      "7       season_4  4.504145e+14\n",
      "8           yr_0 -4.727037e+15\n",
      "9           yr_1 -4.727037e+15\n",
      "10        mnth_1 -1.852956e+15\n",
      "11        mnth_2 -1.852956e+15\n",
      "12        mnth_3 -1.852956e+15\n",
      "13        mnth_4 -1.852956e+15\n",
      "14        mnth_5 -1.852956e+15\n",
      "15        mnth_6 -1.852956e+15\n",
      "16        mnth_7 -1.852956e+15\n",
      "17        mnth_8 -1.852956e+15\n",
      "18        mnth_9 -1.852956e+15\n",
      "19       mnth_10 -1.852956e+15\n",
      "20       mnth_11 -1.852956e+15\n",
      "21       mnth_12 -1.852956e+15\n",
      "22     holiday_0  6.862228e+15\n",
      "23     holiday_1  1.016732e+15\n",
      "24     weekday_0 -6.541530e+15\n",
      "25     weekday_1 -6.960343e+14\n",
      "26     weekday_2 -6.960343e+14\n",
      "27     weekday_3 -6.960343e+14\n",
      "28     weekday_4 -6.960343e+14\n",
      "29     weekday_5 -6.960343e+14\n",
      "30     weekday_6 -6.541530e+15\n",
      "31  workingday_0 -4.759562e+16\n",
      "32  workingday_1 -5.344112e+16\n",
      "33  weathersit_1 -4.658187e+16\n",
      "34  weathersit_2 -4.658187e+16\n",
      "35  weathersit_3 -4.658187e+16\n"
     ]
    }
   ],
   "source": [
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(feat_names), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)\n",
    "print(fs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 0.8268697416944121\n",
      "The r2 score of LinearRegression on train is 0.8503387407241705\n"
     ]
    }
   ],
   "source": [
    "# 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "#测试集\n",
    "print( 'The r2 score of LinearRegression on test is', r2_score(y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print( 'The r2 score of LinearRegression on train is', r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAFsCAYAAADos0H8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHqVJREFUeJzt3X+cVXW97/HXRxihIwKiYAgVWFr+BGw0uKVRlFqYeB630q4pHUl72I9rdeukecvp1zl19dbNm6cON0tMS5RTB089OuklOZa3NCgyDQ0yTZRgpPyBZQp+7h97gR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G9B4iIth///13+cyIIS1JhTCg9yzNeD8NaUmSCuU1aUkqUEfH7t/fkCFDOOqoo9i0aROTJ0/mG9/4BqNHj+71a73zne/kgx/8IIcffvg25VdddRXLli3jS1/6Uq/3CTBixAg2btzY0LozZ87ksssuo729fWvZsmXLuPrqq7n88sv79PoDwZ60JAmA5z3veaxYsYK77rqLMWPGcMUVV/RpP1/96ld3COgStLe393tAb968uan7M6QlSTuYMWMGDz300Nb5Sy+9lGOPPZajjz6aSy65BIAnn3yS2bNnM2XKFI488kgWLlwI1HqxW4aA/vrXv86hhx7Kq1/9am677bat+3vHO97BokWLts6PGDECgI0bNzJr1iyOOeYYjjrqKBYvXrxD3dauXcsJJ5zA1KlTOfLII/nRj37U0DEtXbqUU045BYCOjg7OOeccZs6cycEHH7xNeF9zzTUcd9xxTJ06lXe9611bg/f888+nvb2dI444YmsbQG3I609+8pO86lWv4oYbbmioLo3ydLckaRubN29myZIlzJs3D4CbbrqJVatWcccdd5CZnHrqqdx66610dnZy0EEH8b3vfQ+Axx57bJv9rF27lksuuYTly5czatQoXvOa1zBt2rSdvvbw4cP5zne+w8iRI3nkkUeYPn06p5566jY3YX3zm9/kpJNO4uKLL2bz5s38+c9/7tNx3nPPPdxyyy088cQTvPSlL+X8889n9erVLFy4kNtuu422tjbe/e53c+2113L22Wfzmc98hjFjxrB582ZmzZrFnXfeydFHH7213j/+8Y/7VI+dMaQlSQD85S9/YerUqdx///28/OUv5/Wvfz1QC+mbbrppa8Bu3LiRVatWcfzxx/OhD32Ij3zkI5xyyikcf/zx2+zv9ttvZ+bMmYwdW/uHT6effjq/+c1vdlqHzOSjH/0ot956K3vttRcPPfQQ69at4/nPf/7WdY499ljOOeccnnnmGU477TSmTp3ap+OdPXs2w4YNY9iwYYwbN45169axZMkSli9fzrHHHru1TcaNGwfA9ddfz/z589m0aRNr167l17/+9daQPv300/tUh554uluSBDx3TfqBBx7g6aef3npNOjO56KKLWLFiBStWrGD16tXMmzePQw89lOXLl3PUUUdx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0UQvoazPz21Vxy7UDQGY+GhFLqV2T7u7zvKUN1kTEUGAUtcsmTf28tERPemci4pC62VOBe6rpG4Gzq7sYpwOPVad7fgCcGBH7VXc6ngj8oFr2RERMr65LnA0s3n1H0ncRcTLwEeDUzPxz3aIbgTOquxgnA4dQuzmuyyFhq3C/BXhztf1cBkkb7IRt0JpDAN9I7b2Dbd/DXn0v7O5K91X1nXUlsDIzP1+3qGXaISLGRvWXLRHxPOB11K7Nd/d5rm+bNwM/rD7/3X1n9E1/3Sk3WB7UfnO8C7gT+DdgQj53p98V1K5J/Ipt7/g9h9rNAKuBv6srb6/29VvgS1QjupX+qI7jQWBF9fhK3bKLq+O5l7q71and3fmbatnFdeUHVz+Qq4EbgGEDfXwNtsHfUvsN+K/AOmq/eLVUG/TQPl0e657wAL4FrAWeqX4G5lG7trgEWFU9j6nW7fX3wmB4AK+idkr2zrrvgTe2UjsARwO/qNrgLuDjVXmXn2dgeDW/ulp+cN2+uvzO6MvDYUElSSpUy5/uliSpVIa0JEmFMqQlSSqUIS1JUqEMaUmSCmVIS5JUKENakqRC/X9wouNAFQEBpQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#在训练集上观察预测残差的分布，看是否符合模型假设：噪声为0均值的高斯噪声\n",
    "f, ax = plt.subplots(figsize=(7, 5)) \n",
    "f.tight_layout() \n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAARgAAADQCAYAAADcQn7hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdh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RGqZgDMNIDVMwhmGkhikYwzBSwxSMYRipYQrGMIzU+P+OSUEbNXJ06gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#还可以观察预测值与真值的散点图\n",
    "\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.scatter(y_train, y_train_pred_lr)\n",
    "plt.plot([-3, 3], [-3, 3], '--k')   #数据已经标准化，3倍标准差即可\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True Time')\n",
    "plt.ylabel('Predicted Time')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of RidgeCV on test is 0.8265003608959155\n",
      "The r2 score of RidgeCV on train is 0.8494587408435786\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#正则化的线性回归（L2正则 --> 岭回归）\n",
    "#1. 设置超参数（正则参数）范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    "\n",
    "#2. 生成一个RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)\n",
    "\n",
    "#3. 模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    "\n",
    "#4. 预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print( 'The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))\n",
    "print( 'The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.8307474595613615\n",
      "The r2 score of LassoCV on train is 0.8492101476080866\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#正则化的线性回归（L1正则 --> Lasso）\n",
    "\n",
    "#1 生成LassoCV实例（默认超参数搜索范围）\n",
    "lasso = LassoCV()  \n",
    "\n",
    "#2. 训练（内含CV）\n",
    "lasso.fit(X_train, y_train)  \n",
    "\n",
    "#3. 测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "\n",
    "#4 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print( 'The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print( 'The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
